package org.jmh.org.controller;

import org.jmh.base.annotation.JmhPermission;
import org.jmh.base.util.AjaxResult;
import org.jmh.base.util.PageInfo;
import org.jmh.org.domain.Department;
import org.jmh.org.query.DepartmentQuery;
import org.jmh.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.DeclareRoles;
import java.util.List;
import java.util.Objects;

//@Controller
@Api(value = "部门管理",description = "部门操作的CRUD")
@RequestMapping("/department")
@RestController  // 该controller所有的接口均返回数据
@JmhPermission(name="部门管理",desc = "管理部门的CRUD")
public class DepartmentController {
    @Autowired
    private IDepartmentService service;

    /**
     * put 和post 请求 不能这样直接拿值 要从 requestBody 里面拿值
     * @param department
     * @return
     */
    //保存接口
    @ApiOperation(value = "更新或新增")
    @PutMapping
    @JmhPermission(name="部门保存",desc = "管理部门的新增和修改")
    public AjaxResult save(@RequestBody  Department department){
        try {
            if (Objects.isNull(department.getId())){
                service.insert(department);
            }else{
                service.update(department);
            }
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }

    }

    // 删 delete接口
    @DeleteMapping("/{id}")
    @JmhPermission(name="部门删除",desc = "管理部门的删除")
    public AjaxResult delete(@PathVariable("id") Long id){
        // 有返回值
        try {
            service.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    // 查全部 get
    @GetMapping
    @JmhPermission(name="查询所有部门",desc = "管理部门不分页")
    public AjaxResult selectList(){
        // 有返回值
        try {
            List<Department> departments = service.selectAll();
            return AjaxResult.success(departments);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }


    }

    // 查一个 get
    @GetMapping("/{id}")
    @JmhPermission(name="查询指定部门",desc = "根据id查询部门")
    public AjaxResult selectByID(@PathVariable("id") Long id){
        try {
            Department department = service.selectById(id);
            return AjaxResult.success(department);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }


    // 分页查询 post
    @PostMapping
    @JmhPermission(name="分页查询",desc = "部门的分页查询带高级查询功能")
    public AjaxResult queryPage(@RequestBody DepartmentQuery departmentQuery){
        try {
            PageInfo pageInfo = service.queryPage(departmentQuery);
            return AjaxResult.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    // 批量删除 delete
    @PatchMapping
    @JmhPermission(name="批量删除",desc = "管理部门的批量删除")
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        // 有返回值
        try {
            service.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    // 查一个 get
    @GetMapping("/deptTree")
    @JmhPermission(name="部门树查询",desc = "查询部门树为父部门提供数据")
    public AjaxResult getDeptTree(){
        try {
            List<Department> department = service.getDeptTree();
            return AjaxResult.success(department);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

}
